﻿using Cysharp.Threading.Tasks;
using Cysharp.Threading.Tasks.CompilerServices;
using Cysharp.Threading.Tasks.Linq;
using Cysharp.Threading.Tasks.Triggers;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
public class Sixteen : MonoBehaviour
{

    public GameObject ballPrefab;
    public Transform bornPoint;

    [SerializeField]
    private float speed = 5f;


    private void Start()
    {
        UniTaskTest().Forget();
    }


    //移动方法见脚本Eight
    public async UniTaskVoid Move(Transform tf, CancellationToken token)
    {
        float totalTime = 5;
        float currentTime = 0;


        while (currentTime < totalTime)
        {
            currentTime += Time.deltaTime;
            tf.position += new Vector3(1, 0, 0) * Time.deltaTime;

            await UniTask.NextFrame(token);
        }

    }

    public async UniTaskVoid UniTaskTest()
    {
        //生成小球
        var ball = GameObject.Instantiate(ballPrefab, bornPoint.position, Quaternion.identity);

        //小球移动
        var ballToken = ball.GetCancellationTokenOnDestroy();
        Move(ball.transform, ballToken).Forget();

        var source = new UniTaskCompletionSource<Collision>();

        ball.transform.GetAsyncCollisionEnterTrigger().ForEachAsync((col) =>
        {

            Debug.Log(col.gameObject.name);
        }, cancellationToken: ballToken).Forget();



    }

}
